import Vue from 'vue'
import ajax from '../assets/ajax.js'

const SET_ORDER_HISTORY = 'SET_ORDER_HISTORY'
const GET_ORDER_HISTORY = 'GET_ORDER_HISTORY'
const GET_ORDER_HISTORY_ERROR = 'GET_ORDER_HISTORY_ERROR'
const state = {
  data: null
}

const mutations = {
  [SET_ORDER_HISTORY](state, data) {
    state.data = data
  },
  [GET_ORDER_HISTORY_ERROR](state) {
    // TODO
  }
}

const actions = {
  [GET_ORDER_HISTORY]({ commit }, params) {
    const data = ajax.handleData({
      methodId: '1021',
      data: params
    })

    return new Promise((resolve, reject) => {
      Vue.http.post(ajax.url, data).then((response) => {
        const code = response.body.code
        if (code === '0') {
          const data = response.body.res.data
          commit('SET_ORDER_HISTORY', data)
          resolve()
        }
      }, (error) => {
        commit('GET_ORDER_HISTORY_ERROR')
      })
    })
  }
}

export default {
  state,
  mutations,
  actions
}
